/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gradius;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Administrator
 */
public class GradiusGame extends Canvas
{
    /*
     * Variables
     */
    private JFrame container = new JFrame("Gradius");
    private JPanel gamePanel = (JPanel) container.getContentPane();

    //16:9
    private int panelWidth   = 1280;
    private int panelHeight  = 720;
    private int moveSpeed    = 300;
    
    private KeyboardListener KBL = new KeyboardListener();
    private BufferStrategy strategy;
    private Graphics2D g;
    
    private boolean gameRunning = true;
    private boolean wPressed = false;
    private boolean aPressed = false;
    private boolean sPressed = false;
    private boolean dPressed = false;
    private boolean firing   = false;
    
    private PlayerShip playerShip;
    
    /**
     * 
     */
    GradiusGame()
    {
        this.addKeyListener(KBL);
        
        container.setSize(panelWidth, panelHeight);
        
        gamePanel.setPreferredSize(new Dimension(panelWidth, panelHeight));
        gamePanel.setLayout(null);
        gamePanel.setBackground(Color.black);
        setBounds(0,0,panelWidth, panelHeight);
        
        gamePanel.add(this);

        // Tell AWT not to bother repainting our canvas since we're
        // going to do that our self in accelerated mode
        setIgnoreRepaint(true);

        // finally make the window visible
        container.pack();
        container.setLocationRelativeTo(null);
        container.setResizable(false);
        container.setVisible(true);

        // add a listener to respond to the user closing the window. If they
        // do we'd like to exit the game
        container.addWindowListener(new WindowAdapter() 
        {
            @Override
            public void windowClosing(WindowEvent e) 
            {
                    System.exit(0);
            }
        });

        // request the focus so key events come to us
        requestFocus();

        // create the buffering strategy which will allow AWT
        // to manage our accelerated graphics
        createBufferStrategy(2);
        strategy = getBufferStrategy();
        
        initialize();
    }
    
    private void initialize()
    {
        playerShip = new PlayerShip(this, 20, panelHeight/2);
    }
    
    private void startGameLoop() 
    {
        long lastLoopTime = System.currentTimeMillis();
        
        while (gameRunning)
        {  
            long delta = System.currentTimeMillis() - lastLoopTime;
            lastLoopTime = System.currentTimeMillis();
            
            g = (Graphics2D) strategy.getDrawGraphics();
            //high graphics settings :)
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_RENDERING,
                    RenderingHints.VALUE_RENDER_QUALITY);
            
            g.setColor(Color.black);
            g.fillRect(0,0,panelWidth,panelHeight);
            
            /*
            * Start Drawing 
            */
            
            playerShip.draw(g);
            
            playerShip.move(delta);
            //g.drawImage(playerShip.getImage(), playerShip.getX(), playerShip.getY(), this);
            
            /*
            * End Drawing 
            */
            
            //clear up the graphics and flip the buffer over
            //g.dispose();
            strategy.show();

            playerShip.setHorizontalMovement(0);
            playerShip.setVerticalMovement(0);
            
            if ((aPressed) && (!dPressed)) 
            {
                playerShip.setHorizontalMovement(-moveSpeed);
            } 
            else if ((dPressed) && (!aPressed)) 
            {
                playerShip.setHorizontalMovement(moveSpeed);
            }
            
            if ((wPressed) && (!sPressed)) 
            {
                playerShip.setVerticalMovement(-moveSpeed);
            }
            else if ((sPressed) && (!wPressed)) 
            {
                playerShip.setVerticalMovement(moveSpeed);
            }
            
            if(firing)
            {
                System.out.println("Fire");
            }
            
            // finally pause for a bit. Note: this should run us at about
            // 100 fps but on windows this might vary each loop due to
            // a bad implementation of timer
            try { Thread.sleep(10); } catch (Exception e) {}
        }
    }
    
    private class KeyboardListener implements KeyListener
    {

        @Override
        public void keyTyped(KeyEvent e) 
        {
            //
        }

        @Override
        public void keyPressed(KeyEvent e) 
        {
            if(e.getKeyCode() == KeyEvent.VK_W)
            {
                wPressed = true;
            }
            
            if(e.getKeyCode() == KeyEvent.VK_A)
            {
                aPressed = true;
            }
            
            if(e.getKeyCode() == KeyEvent.VK_S)
            {
                sPressed = true;
            }
            
            if(e.getKeyCode() == KeyEvent.VK_D)
            {   
                dPressed = true;
            } 
            
            if(e.getKeyCode() == KeyEvent.VK_SPACE)
            {   
                firing   = true;
            } 
        }

        @Override
        public void keyReleased(KeyEvent e) 
        {
            if(e.getKeyCode() == KeyEvent.VK_W)
            {
                wPressed = false;
            }
            
            if(e.getKeyCode() == KeyEvent.VK_A)
            {
                aPressed = false;
            }
            
            if(e.getKeyCode() == KeyEvent.VK_S)
            {
                sPressed = false;
            }
            
            if(e.getKeyCode() == KeyEvent.VK_D)
            {   
                dPressed = false;
            } 
            
            if(e.getKeyCode() == KeyEvent.VK_SPACE)
            {   
                firing   = false;
            } 
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        GradiusGame gradiusGame = new GradiusGame();
        
        gradiusGame.startGameLoop();
    }
}
